package com.jhs.kjs.dto;

import java.util.Date;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import com.jhs.kjs.dto.util.abstractDTO;
import com.jhs.kjs.handler.exception.AldangMessegeException;

public class UserDTO extends abstractDTO {
	private Integer seq;
	private Long userIndex;
	private String userId;
	private String userPw;
	private String userName;
	private String cellPhone;
	private String email;
	private Integer level;
	private Long point;
	private Date regDate;
	private String cell1, cell2, cell3, secureCode;
	private String nickName;
	private String zip1;
	private String addr1;
	private Integer agreeMailing=0;
	private Integer agreePublicMyInfo=0;
	/**
	 * 0: 가입<br />
	 * 10 : 승인<br />
	 * 20 : blocked
	 * 
	 */
	private Integer status;
	
	
	
	public Integer getSeq() {
		return seq;
	}

	public void setSeq(Integer seq) {
		this.seq = seq;
	}

	public Long getUserIndex() {
		return userIndex;
	}

	public void setUserIndex(Long userIndex) {
		this.userIndex = userIndex;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		userId = subString(userId, 60);
		this.userId = userId;
	}

	public String getUserPw() {
		return userPw;
	}

	public void setUserPw(String userPw) {
		userPw = subString(userPw, 60);
		this.userPw = userPw;
	}

	public String getCombinedUserPw() {
		return userId + userPw;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		userName = subString(userName, 60);
		this.userName = userName;
	}

	public String getCellPhone() {
		if (cellPhone == null) {
			combineCellPhone(this);
		}
		return cellPhone;
	}

	private void combineCellPhone(UserDTO user) {
		String message = null;
		String cell1 = user.getCell1();
		String cell2 = user.getCell2();
		String cell3 = user.getCell3();

		if (cell1 == null || cell2 == null || cell3 == null) {
			message = "전화번호가 없습니다.";
			throw new AldangMessegeException(message);
		}
		try {
			Integer.parseInt(cell1);
			Integer.parseInt(cell2);
			Integer.parseInt(cell3);
		} catch (NumberFormatException n) {
			message = "전화번호가 올바르지 않습니다.";
			throw new AldangMessegeException(message);
		}

		if (cell1.length() != 3) {
			message = "전화번호가 올바르지 않습니다.";
			throw new AldangMessegeException(message);
		}
		if (cell2.length() != 3 && cell2.length() != 4) {
			message = "전화번호가 올바르지 않습니다.";
			throw new AldangMessegeException(message);
		}
		if (cell3.length() != 4) {
			message = "전화번호가 올바르지 않습니다.";
			throw new AldangMessegeException(message);
		}

		user.setCellPhone(cell1 + "-" + cell2 + "-" + cell3);
	}

	public void setCellPhone(String cellPhone) {
		cellPhone = subString(cellPhone, 20);
		this.cellPhone = cellPhone;
	}

	public String getEmail() {
		InternetAddress emailAddr;
		try {
			emailAddr = new InternetAddress(this.email);
			emailAddr.validate();
		} catch (AddressException e) {
			throw new AldangMessegeException("이메일이 잘못되었습니다.");
		} catch (NullPointerException e) {
			throw new AldangMessegeException("이메일이 없습니다.");
		}
		return email;
	}

	public void setEmail(String email) {
		email = subString(email, 127);
		this.email = email;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Long getPoint() {
		return point;
	}

	public void setPoint(Long point) {
		this.point = point;
	}

	public Date getRegDate() {
		return regDate;
	}

	public void setRegDate(Date regDate) {
		this.regDate = regDate;
	}

	public String getCell1() {
		return cell1;
	}

	public void setCell1(String cell1) {
		cell1 = subString(cell1, 3);
		this.cell1 = cell1;
	}

	public String getCell2() {
		return cell2;
	}

	public void setCell2(String cell2) {
		cell2 = subString(cell2, 4);
		this.cell2 = cell2;
	}

	public String getCell3() {
		return cell3;
	}

	public void setCell3(String cell3) {
		cell3 = subString(cell3, 4);
		this.cell3 = cell3;
	}

	public String getSecureCode() {
		return secureCode;
	}

	public void setSecureCode(String secureCode) {
		secureCode = subString(secureCode, 4);
		this.secureCode = secureCode;
	}

	public String getNickName() {
		return nickName;
	}

	public String getZip1() {
		return zip1;
	}

	public void setZip1(String zip1) {
		zip1 = subString(zip1, 60);
		this.zip1 = zip1;
	}

	public String getAddr1() {
		return addr1;
	}

	public void setAddr1(String addr1) {
		addr1 = subString(addr1, 60);
		this.addr1 = addr1;
	}

	public void setNickName(String nickName) {
		nickName = subString(nickName, 45);
		this.nickName = nickName;
	}

	private String subString(String string, int limit) {
		if(string != null){
			if(string.length()>45)
				string = string.substring(0, limit);
		}
		return string;
	}

	public Integer getAgreeMailing() {
		return agreeMailing;
	}

	public void setAgreeMailing(Integer agreeMailing) {
		this.agreeMailing = agreeMailing;
	}

	public Integer getAgreePublicMyInfo() {
		return agreePublicMyInfo;
	}

	public void setAgreePublicMyInfo(Integer agreePublicMyInfo) {
		this.agreePublicMyInfo = agreePublicMyInfo;
	}
	/**
	 * 0: 가입<br />
	 * 10 : 승인<br />
	 * 20 : blocked
	 * 
	 */
	public Integer getStatus() {
		return status;
	}
	/**
	 * 0: 가입<br />
	 * 10 : 승인<br />
	 * 20 : blocked
	 * 
	 */
	public void setStatus(Integer status) {
		this.status = status;
	}

}
